package com.ruoyi.web.controller.note;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.note.domain.NoteDocument;
import com.ruoyi.note.service.INoteDocumentService;

/**
 * 文档管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@RestController
@RequestMapping("/note/document")
public class NoteDocumentController extends BaseController
{
    @Autowired
    private INoteDocumentService noteDocumentService;

    /**
     * 查询文档管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:document:list')")
    @GetMapping("/list")
    public TableDataInfo list(NoteDocument noteDocument)
    {
        startPage();
        List<NoteDocument> list = noteDocumentService.selectNoteDocumentList(noteDocument);
        return getDataTable(list);
    }

    /**
     * 导出文档管理列表
     */
    @PreAuthorize("@ss.hasPermi('note:document:export')")
    @Log(title = "文档管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NoteDocument noteDocument)
    {
        List<NoteDocument> list = noteDocumentService.selectNoteDocumentList(noteDocument);
        ExcelUtil<NoteDocument> util = new ExcelUtil<NoteDocument>(NoteDocument.class);
        util.exportExcel(response, list, "文档管理数据");
    }

    /**
     * 获取文档管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:document:query')")
    @GetMapping(value = "/{documentId}")
    public AjaxResult getInfo(@PathVariable("documentId") Long documentId)
    {
        return success(noteDocumentService.selectNoteDocumentByDocumentId(documentId));
    }

    /**
     * 新增文档管理
     */
    @PreAuthorize("@ss.hasPermi('note:document:add')")
    @Log(title = "文档管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NoteDocument noteDocument)
    {
        noteDocument.setCreateBy(getUsername());
        return toAjax(noteDocumentService.insertNoteDocument(noteDocument));
    }

    /**
     * 修改文档管理
     */
    @PreAuthorize("@ss.hasPermi('note:document:edit')")
    @Log(title = "文档管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NoteDocument noteDocument)
    {
        noteDocument.setUpdateBy(getUsername());
        return toAjax(noteDocumentService.updateNoteDocument(noteDocument));
    }

    /**
     * 删除文档管理
     */
    @PreAuthorize("@ss.hasPermi('note:document:remove')")
    @Log(title = "文档管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{documentIds}")
    public AjaxResult remove(@PathVariable String documentIds)
    {
        return toAjax(noteDocumentService.deleteNoteDocumentByDocumentIds(documentIds));
    }

    /**
     * 增加阅读次数
     */
    @PostMapping("/view/{documentId}")
    public AjaxResult increaseViewCount(@PathVariable("documentId") Long documentId)
    {
        return toAjax(noteDocumentService.increaseViewCount(documentId));
    }
}